<script>
	import {
		loginByWxCode,
		getWxMyUserInfo,
		publicConfig,
		loginByCode,
		pageConfig
	} from './utils/http/http.js'

	export default {
		globalData: {
			pages: 'text'
		},
		 onLaunch: function(options) {
			console.log(options,'参数')
			uni.setStorageSync('onLaunchData',options.query);
			this.$pages = ''

			// this.publicConfig()
			// console.log('onlaunch 获取pages', this.$pages)
			// console.warn('当前组件仅支持 uni_modules 目录结构 ，请升级 HBuilderX 到 3.1.0 版本以上！')
			/*#ifdef MP*/
			// 在小程序的某个页面中调用登录接口获取code
			this.getWXLogin()
			/*#endif*/
			
			// #ifdef APP|H5
			this.getPublicConfig(options)
			//判断是否是有code的
			// this.block(2000)
			// #endif
		},

		onShow: function() {
			this.$pages = 'helloonshow'
			// console.log(this.$pages)
			//    console.log('App Show2222222222222')
			// this.getWxMyUserInfo()
		},

		onHide: function() {
			console.log('App Hide')
		},

		methods: {

			async getPublicConfig(options){
				
				await this.h5Login(options)
				
				if (uni.getStorageSync('publicConfig')) {
					this.$isResolve()
					return
				}
				
				
				
				const res = await pageConfig();
				uni.setStorageSync('publicConfig', res.data)
				this.$isResolve()
			},
			async h5Login(options){
				const token =uni.getStorageSync('token');
				const userAgent = navigator.userAgent.toLowerCase();
				     // 检查用户代理字符串中是否包含 'micromessenger'
				const isInWeChat = userAgent.indexOf('micromessenger')!== -1;
				
				if(token || !isInWeChat){
					return;
				}
				
				if(options.query.code){
					const code = options.query.code;
					console.log('获取到了code要去登录'+ options.query.code)
					const res = await loginByWxCode({
						code
					});
					console.log('到了获取页面',res)
					// 把token和user和social存储到本地
					uni.setStorageSync('token', res.data.token);
					uni.setStorageSync('user', res.data.user);
					uni.setStorageSync('social', res.data.social);
					uni.setStorageSync('publicConfig', res.data.publicConfig)
					// console.log('刷新以页面')
					this.$isResolve()
					// location.reload()
					return;
				}
				
				
				//h5 oauth2 登录逻辑
			    const appId = 'wxacd3f9be8282b278'; // 替换为你的 AppID
			    const redirectUri = encodeURIComponent('https://soft.511m.com/h5_callback'); // 替换为你的回调页面 URL
			    const scope = 'snsapi_userinfo'; // 授权作用域，snsapi_userinfo 可获取用户详细信息
			    const state = 'local'; // 自定义的状态参数，用于防止 CSRF 攻击
			    const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
			
			    window.location.href = authUrl;
				//跳转登录
				console.log('跳转登录')
				
			},
			async getWXLogin() {
				const that = this;
				//有缓存，先取缓存
				// console.log('读取缓存',uni.getStorageSync('publicConfig'))
				if (uni.getStorageSync('publicConfig')) {
					that.$isResolve()
				}
				uni.login({
					success: async res => {
						const code = res.code;
						// 调用登录接口 
						try {
							if (typeof tt !== 'undefined') {
								// 抖音环境
								const res = await loginByCode({
									code
								})
								uni.setStorageSync('token', res.data.token);
								// uni.setStorageSync('token','MWI0OWM5MDFlMjhmYzhjMDE0ZGVmZTA4MTg0MzQ1NjE=')
								uni.setStorageSync('user', res.data.user);
								uni.setStorageSync('social', res.data.social);
								this.getWxMyUserInfo()
							} else {
								// 微信小程序环境
								const res = await loginByWxCode({
									code
								});
								// 把token和user和social存储到本地
								uni.setStorageSync('token', res.data.token);
								uni.setStorageSync('user', res.data.user);
								uni.setStorageSync('social', res.data.social);
								uni.setStorageSync('publicConfig', res.data.publicConfig)
								// console.log(res.data.publicConfig)
								that.$isResolve()
								this.getWxMyUserInfo()

							}

						} catch (err) {
							console.error(err);
						}
					},
					fail: err => {
						console.error(err);
					}
				});
			},
		
			async getWxMyUserInfo() {
				const res = await getWxMyUserInfo()
				if (res.data.user === undefined) {
					uni.setStorageSync('is_vip', res.data.user.is_vip)
				} else {
					if (res.data.user === null) {
						// uni.setStorageSync('is_vip',1)
						uni.setStorageSync('is_vip', 0)
					} else {
						uni.setStorageSync('is_vip', res.data.user.is_vip)
						// uni.setStorageSync('is_vip',1)
					}
				}
			},
			async publicConfig() {
				const res = await publicConfig()
				if (res.code === 200) {
					uni.setStorageSync('publicConfig', res.data)
					this.$isResolve()
				}
			}

		}
	}
</script>


<style lang="scss">
	@import "uview-ui/index.scss";
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	@import "@/static/index-icon/iconfont.css";
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';

	// 设置整个项目的背景色
	page {
		background-color: #f5f5f5;
	}

	/* #endif */
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}

	// ios底部安全距离
	/*#ifdef MP*/
	.container {
		padding-bottom: constant(safe-area-inset-bottom);
		/* 兼容 iOS 设备 */
		padding-bottom: env(safe-area-inset-bottom);
		/* 兼容 iPhone X 及以上设备 */
	}

	/*#endif*/
	/* #ifdef APP-PLUS*/
	.container {
		padding-bottom: constant(safe-area-inset-bottom);
		/* 兼容 iOS 设备 */
		padding-bottom: env(safe-area-inset-bottom);
		/* 兼容 iPhone X 及以上设备 */
	}

	/*#endif*/
	// 公共样式书写
	.container {
		height: 100%;
		display: flex;
		align-items: center;
		box-sizing: border-box;
	}

	page {
		font-size: 28rpx;
		color: #333333;
		background-color: #F7F7F7;
	}


	view {
		box-sizing: border-box;
	}

	.bg-default {
		background-color: #F0FFFC;
	}

	.color-default {
		color: #47A195;
	}

	.shadow {
		box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);
	}

	.w-full {
		width: 100%;
	}

	.f24 {
		font-size: 24rpx;
	}

	.f28 {
		font-size: 28rpx;
	}

	.f32 {
		font-size: 32rpx;
	}

	.f36 {
		font-size: 36rpx;
	}

	.f39 {
		font-size: 39rpx;
	}

	.f77 {
		font-size: 77rpx;
	}

	.f42 {
		font-size: 42rpx;
	}

	.text-blod {
		font-weight: bold;
	}

	.color-default {
		color: green;
	}

	.color-333333 {
		color: #333333;
	}

	.color-666666 {
		color: #666666;
	}

	.color-cccccc {
		color: #CCCCCC;
	}

	.color-999999 {
		color: #999999;
	}

	.color-193AC2 {
		color: #193AC2;
	}

	.color-FFFFFF {
		color: #FFFFFF;
	}

	.color-FC453F {
		color: #FC453F;
	}

	.color-23B7D9 {
		color: #23B7D9;
	}

	.bg-193AC2 {
		background-color: #193AC2;
	}


	.bg-DADFF5 {
		background-color: #DADFF5;
	}

	.bg-FC453F {
		background-color: #FC453F;
	}

	.bg-F3F3F3 {
		background-color: #F3F3F3;
	}

	.bg-ffffff {
		background-color: #FFFFFF;
	}

	.bg-CCCCCC {
		background-color: #CCCCCC;
	}

	.bg-fd6b90 {
		background-color: #fd6b90;
	}

	//外边距
	.m-t-b-sm {
		margin: 30rpx 0 !important;
	}

	.m-r-l-sm {
		margin: 0 30rpx !important;
	}

	.m-l-r-sm {
		margin: 0 30rpx !important;
	}

	.m-r-20 {
		margin-right: 20rpx;
	}

	.m-sm {
		margin: 10rpx;
	}

	.p-10 {
		padding: 10rpx;
	}

	.p-20 {
		padding: 20rpx;
	}

	.p-b-20 {
		padding-bottom: 20rpx;
	}

	.m-t-b-30 {
		margin: 30rpx 0 !important;
	}

	.m-t-b-20 {
		margin: 20rpx 0 !important;
	}

	.m-t-40 {
		margin-top: 40rpx !important;
	}

	.m-t-20 {
		margin-top: 20rpx;
	}

	.p32 {
		padding: 0 32rpx;
	}

	.p24 {
		padding: 0 24rpx !important;
	}

	.pb32 {
		padding-bottom: 32rpx;
	}


	.pl20 {
		padding-left: 20rpx !important;
	}

	.pl30 {
		padding-left: 30rpx;
	}

	.pr20 {
		padding-right: 20rpx;
	}

	.p-40-24 {
		padding: 40rpx 24rpx !important;
	}

	.border-193AC2 {
		border: 2rpx solid #193AC2;
	}

	// 灰色的左边线
	.line {
		padding-left: 30rpx;
		border-left: 15rpx solid #a5a5a5;
	}

	// 灰色的下横线
	.line-X {
		padding-bottom: 30rpx;
		border-bottom: 1rpx solid #ccc;
	}

	// 圆角
	.border-radius25 {
		border-radius: 25px
	}

	.border-radius5 {
		border-radius: 5px
	}

	.border-radius10 {
		border-radius: 10px
	}

	// 居中
	.text-Acenter {
		text-align: center;
	}
	.flex{
		display: flex;
	}

	.flex-center {
		
		justify-content: center;
		align-items: center;
	}

	.box-center {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.flex-s-b {
		display: flex;
		justify-content: space-between;
	}
	
	.flex-end {
		display: flex;
		justify-content: flex-end;
	}

	.flex-wrap {
		display: flex;
		flex-wrap: wrap;
	}

	.flex-column {
		display: flex;
		flex-direction: column;
	}

	.monet {
		width: 40rpx;
		height: 40rpx;
		margin-right: 16rpx;
	}

	.monet image {
		width: 100%;
		height: 100%;
	}

	.border-F3F3F3 {
		border: 2rpx solid #F3F3F3
	}

	.bottom38 {
		bottom: 38rpx !important;
	}

	.isIphoneBottom {
		width: 100%;
		height: 38rpx;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		z-index: 9999;
	}

	.omit {
		overflow: hidden;
		/*内容会被修剪，并且其余内容是不可见的*/
		text-overflow: ellipsis;
		/*显示省略符号来代表被修剪的文本。*/
		white-space: nowrap;
	}

	.mask {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		background: rgba(0, 0, 0, 0.5);
		opacity: 1;
		overflow: hidden;
		z-index: 999999999;
	}

	/* 安全距离 */
	.footerClass {
		/* iOS 11.0 */
		padding-bottom: constant(safe-area-inset-bottom);
		/* iOS 11.2 */
		padding-bottom: env(safe-area-inset-bottom);
	}

	/* 选择图标大小 */
	.circle {
		width: 48rpx;
		height: 48rpx;
		/* margin: 0 16rpx 0 8rpx; */
	}

	.fw-500 {
		font-weight: 500;
	}

	.fw-bold {
		font-weight: bold;
	}

	.fw-400 {
		font-weight: 400;
	}

	.flex-1 {
		flex: 1;
		min-width: 0;
	}

	/* 导航栏标题文字居中 */
	.custom-navigation-bar {
		text-align: center;
	}

	.color-ff5500 {
		color: #ff5500;
	}

	.marbottom-16 {
		margin-bottom: 14rpx;
	}

	.marbottom-32 {
		margin-bottom: 32rpx;
	}
</style>